﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_ZhanQuXiaoQuGuanLianBiaoService : It_ZhanQuXiaoQuGuanLianBiaoService
    {
        public readonly MyContext _Context;
        public t_ZhanQuXiaoQuGuanLianBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        public int Add(t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao)
        {
            _Context.t_ZhanQuXiaoQuGuanLianBiao.Add(t_ZhanQuXiaoQuGuanLianBiao);
            return _Context.SaveChanges();
        }

        public bool DelFangFa(int id)
        {
            _Context.t_ZhanQuXiaoQuGuanLianBiao.Remove(_Context.t_ZhanQuXiaoQuGuanLianBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public List<t_ZhanQuXiaoQuGuanLianBiao> GetList(t_RenYuanBiao t_RenYuanBiao)
        {
            return _Context.t_ZhanQuXiaoQuGuanLianBiao.Where(x => x.ZhanQuId==t_RenYuanBiao.ZhanQuId).ToList();
        }

        public string GetListWithFenYe(t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_ZhanQuXiaoQuGuanLianBiao);
            var list = new List<t_ZhanQuXiaoQuGuanLianBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_ZhanQuXiaoQuGuanLianBiao.Include(x => x.t_FangYuanBiao).Include(x => x.t_ZhanQuBiao).Where(x => x.XiaoQuId == t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId).ToList();
            }
            else
            {
                list = _Context.t_ZhanQuXiaoQuGuanLianBiao.Include(x => x.t_FangYuanBiao).Include(x => x.t_ZhanQuBiao).ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.Id).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.Id).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_FangYuanBiao.XiaoQuMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_FangYuanBiao.XiaoQuMingCheng).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_ZhanQuBiao.ZhanQuMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_ZhanQuBiao.ZhanQuMingCheng).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public t_ZhanQuXiaoQuGuanLianBiao GetModel(t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao)
        {
            return _Context.t_ZhanQuXiaoQuGuanLianBiao.FirstOrDefault(x => x.XiaoQuId == t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId && x.ZhanQuId == t_ZhanQuXiaoQuGuanLianBiao.ZhanQuId);
        }

        public string ReturnWhere(t_ZhanQuXiaoQuGuanLianBiao t_ZhanQuXiaoQuGuanLianBiao)
        {
            string returnstr = "1=1";
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu1 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu2 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu3 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu4 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu5 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu6 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu7 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu8 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu9 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_ZhanQuXiaoQuGuanLianBiao.BeiZhu10 + ")";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId != null && t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId != 0)
            {
                returnstr = returnstr + " and XiaoQuId == " + t_ZhanQuXiaoQuGuanLianBiao.XiaoQuId + "";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.ZhanQuId != null && t_ZhanQuXiaoQuGuanLianBiao.ZhanQuId != 0)
            {
                returnstr = returnstr + " and ZhanQuId == " + t_ZhanQuXiaoQuGuanLianBiao.ZhanQuId + "";
            }
            if (t_ZhanQuXiaoQuGuanLianBiao.Id != null && t_ZhanQuXiaoQuGuanLianBiao.Id != 0)
            {
                returnstr = returnstr + " and Id == " + t_ZhanQuXiaoQuGuanLianBiao.Id + "";
            }
            if (returnstr.Length > 0)
            {
                return returnstr.Substring(4);
            }
            else
            {
                return "";
            }
        }

    }
}
